package com.hexb.smh.controller.management;

import com.hexb.core.common.Page;
import com.hexb.core.utils.Assert;
import com.hexb.smh.annotations.AopOperationLog;
import com.hexb.smh.entity.Account;
import com.hexb.smh.entity.enums.Operators;
import com.hexb.smh.entity.enums.RoleType;
import com.hexb.smh.entity.param.account.AccountParam;
import com.hexb.smh.entity.param.account.AccountQueryParam;
import com.hexb.smh.entity.view.ImportResult;
import com.hexb.smh.errors.ErrorCodes;
import com.hexb.smh.service.IAccountService;
import com.hexb.smh.utils.OperationConst;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

import static com.hexb.smh.utils.SmhConstant.*;

/**
 * @author : hexb
 */
@RestController
@RequestMapping("/management/account/salesman")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(value = "A3.业务员帐号", description = "Server", tags = {"A3.业务员帐号"})
public class SalesmanController {

    final private IAccountService accountService;


    @ApiOperation("查询业务员(分页)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "分页大小")
            , @ApiImplicitParam(name = "pageNum", value = "当前页(第1页为1)")
    })
    @RequiresRoles(logical = Logical.OR, value = {ROLE_SUPER_ADMIN, ROLE_ADMIN, ROLE_SALESMAN_MANAGER})
    @GetMapping("/{pageSize}/{pageNum}")
    public Page<Account> pageSalesman(AccountQueryParam param,
                                      @PathVariable int pageSize,
                                      @PathVariable int pageNum) {
        param.setRole(RoleType.salesman);
        return accountService.search(param, pageSize, pageNum);
    }


    @AopOperationLog(name = OperationConst.APPEND_SALESMAN, operations = Operators.appendAccount)
    @ApiOperation("添加业务员")
    @RequiresRoles(logical = Logical.OR, value = {ROLE_SUPER_ADMIN, ROLE_ADMIN, ROLE_SALESMAN_MANAGER})
    @PostMapping
    public Account appendSalesman(@RequestBody @Valid AccountParam.AccountParamInsert param) {
        Account account = param.toSalesman();
        accountService.save(account);
        return account;
    }


    @AopOperationLog(name = OperationConst.UPDATE_SALESMAN, operations = Operators.updateAccount)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "帐号Id")
    })
    @ApiOperation("更新业务员")
    @RequiresRoles(logical = Logical.OR, value = {ROLE_SUPER_ADMIN, ROLE_ADMIN, ROLE_SALESMAN_MANAGER})
    @PutMapping("/{id}")
    public Account updateSalesman(@PathVariable Integer id,
                                  @RequestBody @Valid AccountParam.AccountParamUpdate param) {
        Account account = param.toAccount(id);
        accountService.update(account);
        return account;
    }


    @AopOperationLog(name = OperationConst.DELETE_SALESMAN, operations = Operators.deleteAccount)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "帐号Id")
    })
    @ApiOperation("注销(删除)业务员")
    @RequiresRoles(logical = Logical.OR, value = {ROLE_SUPER_ADMIN, ROLE_ADMIN, ROLE_SALESMAN_MANAGER})
    @DeleteMapping("/{id}")
    public int updateSalesman(@PathVariable Integer id) {
        return accountService.delete(id);
    }


    @AopOperationLog(name = OperationConst.ENABLE_SALESMAN, operations = Operators.enableAccount)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "帐号Id")
    })
    @ApiOperation("启用业务员")
    @RequiresRoles(logical = Logical.OR, value = {ROLE_SUPER_ADMIN, ROLE_ADMIN, ROLE_SALESMAN_MANAGER})
    @PutMapping("/enable/{id}")
    public Account enableSalesman(@PathVariable Integer id) {
        return accountService.toggleAccount(id, true);
    }


    @AopOperationLog(name = OperationConst.DISABLE_SALESMAN, operations = Operators.disableAccount)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "帐号Id")
    })
    @ApiOperation("禁用业务员")
    @RequiresRoles(logical = Logical.OR, value = {ROLE_SUPER_ADMIN, ROLE_ADMIN, ROLE_SALESMAN_MANAGER})
    @PutMapping("/disable/{id}")
    public Account disableSalesman(@PathVariable Integer id) {
        return accountService.toggleAccount(id, false);
    }


    @AopOperationLog(name = OperationConst.RESET_SALESMAN, operations = Operators.resetPassword)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "帐号Id")
    })
    @ApiOperation("重置业务员密码")
    @RequiresRoles(logical = Logical.OR, value = {ROLE_SUPER_ADMIN, ROLE_ADMIN, ROLE_SALESMAN_MANAGER})
    @PutMapping("/reset/{id}")
    public int resetSalesman(@PathVariable Integer id) {
        return accountService.restPassword(id);
    }


    @ApiOperation("获取单个业务员信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "业务员id")
    })
    @RequiresRoles(logical = Logical.OR, value = {ROLE_SUPER_ADMIN, ROLE_ADMIN, ROLE_SALESMAN_MANAGER})
    @GetMapping("/{id}")
    public Account getSalesman(
            @PathVariable int id) {
        return accountService.findById(id);
    }

    @AopOperationLog(name = OperationConst.DELETE_SALESMAN, operations = Operators.batchDeleteAccount)
    @ApiOperation("批量删除(注销)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "管理员id列表")
    })
    @RequiresRoles(logical = Logical.OR, value = {ROLE_SUPER_ADMIN, ROLE_ADMIN, ROLE_SALESMAN_MANAGER})
    @DeleteMapping("/batch/{ids}")
    public int getSalesman(
            @PathVariable Integer[] ids) {
        return accountService.deleteBatch(ids);
    }


    @AopOperationLog(name = OperationConst.ENABLE_SALESMAN, operations = Operators.batchEnableAccount)
    @ApiOperation("批量启用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "管理员id列表")
    })
    @RequiresRoles(logical = Logical.OR, value = {ROLE_SUPER_ADMIN, ROLE_ADMIN, ROLE_SALESMAN_MANAGER})
    @PutMapping("/batch/enable/{ids}")
    public int batchEnableSalesman(
            @PathVariable Integer[] ids) {
        return accountService.updateEnableBatch(ids, true);
    }


    @AopOperationLog(name = OperationConst.DISABLE_SALESMAN, operations = Operators.batchDisableAccount)
    @ApiOperation("批量禁用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "管理员id列表")
    })
    @RequiresRoles(logical = Logical.OR, value = {ROLE_SUPER_ADMIN, ROLE_ADMIN, ROLE_SALESMAN_MANAGER})
    @PutMapping("/batch/disable/{ids}")
    public int batchDisableSalesman(
            @PathVariable Integer[] ids) {
        return accountService.updateEnableBatch(ids, false);
    }

    @ApiOperation("导入业务员")
    @RequiresRoles(logical = Logical.OR, value = {ROLE_SUPER_ADMIN, ROLE_ADMIN, ROLE_SALESMAN_MANAGER})
    @PostMapping("/import")
    public ImportResult<Account> importSalesman(MultipartFile file) {
        String fileName = file.getOriginalFilename().toLowerCase();
        Assert.isFalse(fileName.endsWith("xls") || fileName.endsWith("xlsx"), ErrorCodes.NOT_EXCEL_FILE);
        return accountService.importSalesman(file);
    }

}
